﻿using System;
using System.Collections.Generic;
using System.IO;

using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using Lib.Common;
using Lib.Utility;
using Module.BusinessLogic;
using Module.Core.LoginSystem.Commons.Framwork;
using Module.Core.LoginSystem.Commons.Utilities;
using Module.EntitiesInfo;
using ConvertUtility = Lib.Utility.ConvertUtility;

namespace Web.Cms.Gallery
{
    public partial class AddEditGallery : AdminPage
    {
        #region Khai báo
        private int galleryId;
        private CMSGalleryInfo info = new CMSGalleryInfo();
        #endregion

        #region PageLoad
        protected void Page_Load(object sender, EventArgs e)
        {
            try
            {
                galleryId = ConvertUtility.ToInt32(Request.QueryString["galleryId"]) == null ? 0 : ConvertUtility.ToInt32(Request.QueryString["galleryId"]);
                //if (!Directory.Exists(Server.MapPath("/Uploads/" + CurrentUser.UserID)))
                //    Directory.CreateDirectory(Server.MapPath("/Uploads/" + CurrentUser.UserID));
                //txtAvatar.fpUploadDir = txtAvatar.fpUploadDir = txtBigAvatar.fpUploadDir = "/Uploads/" + CurrentUser.UserID;
              

                txtTeaser.SetSecurityFilesGalleryPath(Server.MapPath("/Uploads/" + CurrentUser.UserID));
                txtTeaser.SetSecurityFlashGalleryPath(Server.MapPath("/Uploads/" + CurrentUser.UserID));
                txtTeaser.SetSecurityGalleryPath(Server.MapPath("/Uploads/" + CurrentUser.UserID));
                txtTeaser.SetSecurityImageGalleryPath(Server.MapPath("/Uploads/" + CurrentUser.UserID));
                txtTeaser.SetSecurityMediaGalleryPath(Server.MapPath("/Uploads/" + CurrentUser.UserID));
                txtTeaser.SetSecurityTemplateGalleryPath(Server.MapPath("/Uploads/" + CurrentUser.UserID));



               
                if (!Page.IsPostBack)
                {
                    txtCreateDate.SelectedDate = DateTime.Now;
                    CMSZonesBL.GetDataComboType(dropZoneId, "0", "(" + ModulTypeBL.HinhAnh.Type.ToString() + ")");                   
                    LoadData();
                }
            }
            catch (Exception ex)
            {
            }


        }
        #endregion

        #region LoadData
        private void LoadData()
        {
            try
            {
                if (galleryId > 0)
                {
                    btnUpdateVideo.Visible = true;
                    btnAddVideo.Visible = false;

                    info = CMSGalleryBL.GetInfo(galleryId);
                    if (info != null)
                    {
                        txtTitle.Text = info.Title;
                        ImgAvatar.ImageUrl = info.Avatar;
                        ImgAvatar.Visible = true;
                        txtCreateDate.SelectedDate = info.CreatedOn;
                        txtTeaser.Text = info.Teaser;
                        txtAvatar.Text = info.Avatar;
                        txtBigAvatar.Text = info.BigAvatar;
                        dropZoneId.SelectedValue = info.ZoneId.ToString();

                    }
                }
                else
                {
                    btnUpdateVideo.Visible = false;
                    btnAddVideo.Visible = true;
                    ImgAvatar.Visible = true;

                }
            }
            catch (Exception ex)
            {
                lblUpdateStatus.Text = "Có lỗi xảy ra Error: " + ex.Message;
            }
        }
        #endregion

        #region FillData
        void FillData(CMSGalleryInfo info)
        {

            info.GalleryId = galleryId;
            info.Title = txtTitle.Text;
            info.Teaser = txtTeaser.Text;
            info.Status = 1;
            info.ShowType = 0;
            info.HitCount = 0;
            info.CreatedOn = DateTime.Now;
            info.CreatedBy = CurrentUser.Username;
            info.ZoneId = int.Parse(dropZoneId.SelectedValue.ToString());         
            if (txtAvatar.Text.Trim() != info.Avatar && txtAvatar.Text.Trim().Length > 0)
            {
                info.Avatar = ConvertImage.SetAvatarThumbnailWidthDirection(Server.MapPath(txtAvatar.Text.Trim()), 100, "_avatar");
            }
            else
            {
                if (txtAvatar.Text.Length > 0)
                {
                    info.Avatar = txtAvatar.Text.Trim();
                }
                else
                {
                    info.Avatar = "";
                }
            }
            info.BigAvatar = txtBigAvatar.Text.Trim();


        }
        #endregion

        protected void btnAddVideo_Click(object sender, EventArgs e)
        {
            try
            {
                CMSGalleryInfo info = new CMSGalleryInfo();
                FillData(info);
                if (txtTitle.Text.Length == 0)
                {

                    MessageBox.Show("Bạn chưa nhập tiêu đề ảnh?");
                    txtTitle.Focus();
                    return;
                }
                if (dropZoneId.SelectedIndex == 0)
                {
                    MessageBox.Show("Bạn chưa chọn danh mục?");
                    dropZoneId.Focus();
                    return;
                }
                else
                {
                    int i = CMSGalleryBL.Insert(info);
                    if (i > 0)
                    {
                        lblUpdateStatus.Text = "Bạn đã thêm mới thành công";
                        HttpContext.Current.Response.Write("<script>window.top.hidePopWinADMIN(false);window.parent.location.href='/Cms/Default.aspx?ctrlkey=ListGalleryPublic';</script>");

                    }
                }

            }
            catch (Exception ex)
            {
                lblUpdateStatus.Text = "Error: " + ex.Message;
            }
        }

        protected void btnUpdateVideo_Click(object sender, EventArgs e)
        {
            try
            {
                CMSGalleryInfo info = new CMSGalleryInfo();
                FillData(info);
                if (txtTitle.Text.Length == 0)
                {

                    MessageBox.Show("Bạn chưa nhập tiêu đề ảnh?");
                    txtTitle.Focus();
                    return;
                }
                if (dropZoneId.SelectedIndex == 0)
                {
                    MessageBox.Show("Bạn chưa chọn danh mục?");
                    dropZoneId.Focus();
                    return;
                }
                else
                {
                    CMSGalleryBL.Update(info);
                    HttpContext.Current.Response.Write("<script>window.top.hidePopWinADMIN(false);window.parent.location.href='/Cms/Default.aspx?ctrlkey=ListGalleryPublic';</script>");
                }

            }
            catch (Exception ex)
            {
                lblUpdateStatus.Text = "Error: " + ex.Message;
            }
        }

        protected void btnCanelVideo_Click(object sender, EventArgs e)
        {
            HttpContext.Current.Response.Write("<script>window.top.hidePopWinADMIN(false);window.parent.location.href='/Cms/Default.aspx?ctrlkey=ListGalleryPublic';</script>");
          
        }
        public  void ClearText()
        {
            txtTitle.Text = "";
            txtTeaser.Text = "";
            txtAvatar.Text = "";
            txtBigAvatar.Text = "";
        }
    }
}